package net.wanho.service.impl;

import net.wanho.po.Type;
import net.wanho.mapper.TypeMapper;
import net.wanho.service.TypeService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 类型表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class TypeServiceImpl implements TypeService {

    private final TypeMapper typeMapper;

    /**
     * 类型表分页查询
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Type type) {
        if(ObjectUtil.isNull(type)){
            type = new Type();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Type> typeList = typeMapper.select(type);
        return new PageInfo(typeList);
    }

    /**
     * 根据主键查询类型表
     * @param typeId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Type findById(Serializable typeId) {
        return typeMapper.selectById(typeId);
    }

    /**
     * 类型表新增
     * @param type
     */
    @Override
    public void add(Type type) {
        typeMapper.insert(type);
    }

    /**
     * 修改类型表
     * @param type
     */
    @Override
    public void modify(Type type) {
        typeMapper.update(type);
    }

    /**
     * 根据主键删除类型表
     * @param typeId
     */
    @Override
    public void removeById(Serializable typeId) {
        if(((String)typeId).contains(",")){
            typeMapper.deleteByIds(((String)typeId).split(","));
        }else {
            typeMapper.deleteById(typeId);
        }
    }
}
